#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>

int main()
{
    // 声明全局变量environ是环境变量
    extern char **environ;
    putenv("PRIVATE_ENV=123455");
    pid_t id = fork();
    if(id == 0)// child
    { 
        // char *const str[] = {"ls","-a","-l",NULL};
        printf("before: I am a process,pid:%d,ppid:%d\n",getpid(),getppid());
        
        sleep(5);
        char *const myargv[] = {
          "otherExe",
          "-a",
          "-b",
          "-c",
          NULL 
        };
        char *const myenv[] = {
          "MYVAL=111",
          "MYPATH=/usr/bin/xxx",
          NULL 
        };
        // 程序替换：成功之后，exec后续的代码不会被执行
        // execl("/usr/bin/ls","ls","-a","-l",NULL);
        // execlp("ls","ls","-a","-l",NULL);
        // execv("/usr/bin/ls",str);
        
        // execle:给替换的子进程添加环境变量
        execle("./otherExe","otherExe","-a","-w","-v",NULL,myenv);
        printf("after: I am a process,pid:%d, ppid:%d\n",getpid(),getppid());
        exit(0);
    }
    // father
    pid_t ret = waitpid(id,NULL,0);
    if(ret > 0)
        printf("wait success, father pid:%d, ret id: %d\n",getpid(),ret);
    
    sleep(5);
    return 0;
}
